// 第三人称视角插件
// 废弃了1.x版本使用alias命令绑定按键操作的做法

string author = "Live";
string pluginName = "TPP 第三人称插件";
string version = "2.0";

// 当插件被初始化
void OnPluginInit()
{
	// 设置插件信息
	PluginData::SetAuthor(author);
	PluginData::SetName(pluginName);
	PluginData::SetVersion(version);

	// 提示
	ThePresident.InfoFeed("插件:"+pluginName+"| 版本:"+version+"| 作者:"+author,false);

	Engine.EnableCustomSettings(true);
	// 允许开启第三人称
	Globals.AllowThirdPersonCamera(true);

}

void OnPluginUnload()
{
    // 关闭第三人称
	Globals.AllowThirdPersonCamera(false);
}

void OnProcessRound()
{
	for( int y = 1; y <= Globals.GetMaxClients(); y++ )
	{
		CTerrorPlayer@ pPlayer = ToTerrorPlayer( y );
		if ( pPlayer is null ) continue;
		int nButtonPressed = pPlayer.m_afButtonPressed;
		int nButtonReleased = pPlayer.m_afButtonReleased;
		int nButtonLast = pPlayer.m_afButtonLast;
		// We just pressed it.
		if ( HasButtonFlag( nButtonPressed, IN_ATTACK2 ) )
		{			
			pPlayer.ConsoleCommand("firstperson");
			// Chat.PrintToChat( all, "{limegreen}player has flag {gold}IN_ATTACK2" );
		}
		// When we release our button
		if ( HasButtonFlag( nButtonReleased, IN_ATTACK2 ) )
		{
			pPlayer.ConsoleCommand("thirdperson");
			pPlayer.ConsoleCommand("c_thirdpersonshoulder 1");
			pPlayer.ConsoleCommand("c_thirdpersonshoulderoffset 30");
			pPlayer.ConsoleCommand("c_thirdpersonshoulderdist 10");
			pPlayer.ConsoleCommand("c_thirdpersonshoulderheight 10");
			// Chat.PrintToChat( all, "{limegreen}player has released flag {gold}IN_ATTACK2" );
		}
		// This function saves the previous pressed input, and will spam. If it spams, then it works.
		// if ( HasButtonFlag( nButtonLast, IN_ATTACK2 ) )
		// {
		// 	Chat.PrintToChat( all, "{limegreen}player last flag was {gold}IN_ATTACK2" );
		// }
	}
}

bool HasButtonFlag( const int &in flags, const int &in flag )
{
	return ((flags & flag) == flag);
}